package com.cskaoyan.service.impl;

import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.pojo.MarketIssue;
import com.cskaoyan.bean.pojo.MarketKeyword;
import com.cskaoyan.bean.pojo.MarketKeywordExample;
import com.cskaoyan.bean.vo.IssueListVO;
import com.cskaoyan.bean.vo.KeywordListVO;
import com.cskaoyan.mapper.MarketKeywordMapper;
import com.cskaoyan.service.KeywordService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 商场管理-关键词
 * 创建日期: 2022/04/07 23:32
 *
 * @author ChengWenqi
 */

@Service
public class KeywordServiceImpl implements KeywordService {
    @Autowired
    MarketKeywordMapper keywordMapper;

    @Override
    public KeywordListVO queryKeywordList(BaseParam param, String keyword, String url) {
        Integer page = param.getPage();
        Integer limit = param.getLimit();
        String sort = param.getSort();
        String order = param.getOrder();
        // 开启分页
        PageHelper.startPage(page, limit);

        // 排序
        MarketKeywordExample example = new MarketKeywordExample();
        example.setOrderByClause(sort + " " + order);
        // 模糊查询
        MarketKeywordExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (keyword != null && !("".equals(keyword))) {
            criteria.andKeywordLike("%" + keyword + "%");
        }
        if (url != null && !("".equals(url))) {
            criteria.andUrlLike("%" + url + "%");
        }
        // 执行查询语句
        List<MarketKeyword> keywordList = keywordMapper.selectByExample(example);

        // 获取分页信息
        PageInfo<MarketKeyword> pageInfo = new PageInfo<>(keywordList);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();

        return new KeywordListVO(total, pages, limit, page, keywordList);

    }

    @Transactional
    @Override
    public MarketKeyword createKeyword(MarketKeyword keyword) {
        // 不知道sortOrder是啥，Integer，先设成0
        keyword.setSortOrder(0);
        keyword.setAddTime(new Date());
        keyword.setUpdateTime(new Date());
        // 插入关键字记录，并获得自增的主键，放到id里
        keywordMapper.insertSelective(keyword);
        return keywordMapper.selectByPrimaryKey(keyword.getId());
    }

    @Transactional
    @Override
    public MarketKeyword keywordUpdate(MarketKeyword keyword) {
        // 先根据keyword的id找到旧记录，判断能否更新
        // 规定：如果根据id没找到记录或者已删除，返回null
        MarketKeyword oldKeyword = keywordMapper.selectByPrimaryKey(keyword.getId());
        if (oldKeyword == null || oldKeyword.getDeleted()) {
            return null;
        }
        keyword.setUpdateTime(new Date());
        keywordMapper.updateByPrimaryKeySelective(keyword);
        return keyword;
    }

    @Override
    public int keywordDelete(MarketKeyword keyword) {
        keyword.setDeleted(true);
        keyword.setUpdateTime(new Date());
        int update = keywordMapper.updateByPrimaryKey(keyword);
        if (update == 1) {
            return 200;
        }
        return 404;
    }
}
